package com.blazingredfireball.rouleurlog.ria.command
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.blazingredfireball.rouleurlog.ria.business.UserDelegate;
	import com.blazingredfireball.rouleurlog.ria.event.RegisterEvent;
	import com.blazingredfireball.rouleurlog.ria.model.RouleurlogModelLocator;
	
	import mx.controls.Alert;
	import mx.logging.Log;
	import mx.rpc.IResponder;
	import mx.rpc.events.FaultEvent;

	public class RegisterCommand implements ICommand, IResponder
	{
		private var model : RouleurlogModelLocator = RouleurlogModelLocator.getInstance();
		private var successEffect : Function;
		
		public function execute(event : CairngormEvent) : void
		{
			var delegate : UserDelegate = new UserDelegate( this );    
			var registerEvent : RegisterEvent = RegisterEvent( event );
						
			delegate.registerUser(registerEvent.username, registerEvent.password);
		}
		
		public function result(data : Object) : void
		{
			if (data.result)
			{
				model.user = data.result;
				model.viewState = RouleurlogModelLocator.MANAGE_RIDES_VIEW;
			}
			else
			{
				Alert.show( "Registration Failed!" );
			}			
		}
		
		public function fault(info : Object) : void
		{
			var faultEvent : FaultEvent = FaultEvent( info );
			Alert.show("Registration Failed!");
		}
		
	}
}